﻿using Prism.Commands;
using Base.ModuleBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Service.Net.Model;
using Service.Net.Manager;
using System.Collections.ObjectModel;
using Base.Entities.Model;
using SignalRClientTool.Models;
using SignalRClientTool.Views;
using System.Windows.Data;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using Base.Entities.Enums;
using System.Windows.Forms;

namespace SignalRClientTool.ViewModels
{
    public class ClientToolWindowViewModel : ViewModelBase
    {
        private readonly INetManager _netManager;
        private readonly IClientToolManager _clientToolManager;

        public ClientToolWindowViewModel(INetManager netManager, IClientToolManager clientToolManager)
        {
            _netManager = netManager;
            _clientToolManager = clientToolManager;

            Initialize();
        }

        private bool? _IsConnected = false;
        public bool? IsConnected { get => _IsConnected; set => SetProperty(ref _IsConnected, value); }

        private string subText;
        public string SubText { get => subText; set => SetProperty(ref subText, value); }

        private string sendText;
        public string SendText { get => sendText; set => SetProperty(ref sendText, value); }

        private DelegateCommand _ConfigCommand;
        public DelegateCommand ConfigCommand => _ConfigCommand ?? (_ConfigCommand = new DelegateCommand(OnConfigCommand, CanConnectCommand));

        private void OnConfigCommand()
        {
            MessageHelper.ShowView(nameof(NetConfigView));
        }

        private DelegateCommand _ConnectCommand;
        public DelegateCommand ConnectCommand => _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(OnConnectCommand, CanConnectCommand));

        private bool CanConnectCommand()
        {
            return IsConnected == false;
        }

        private async void OnConnectCommand()
        {
            try
            {
                IsConnected = null;
                RaiseCommand();
                var tempInfo = _clientToolManager.GetNetConfig();
                await _netManager.ConnectNet(tempInfo);
            }
            catch (Exception ex)
            {
                _logger.Debug($"connect error {ex}");
            }
        }

        private DelegateCommand _SendCommand;
        public DelegateCommand SendCommand => _SendCommand ?? (_SendCommand = new DelegateCommand(OnSendCommand, CanSendCommand));

        private bool CanSendCommand()
        {
            return IsConnected == true;
        }

        private async void OnSendCommand()
        {
            AddOutValue($"发送消息：{SendText}");
            await _netManager.SendMessage("ServerMessageAsync", SendText);
        }

        private void Initialize()
        {
            _netManager.MessageEvent += OnMessageEvent;
            _netManager.ConnectStatusChangedEvent += OnConnectStatusChangedEvent;
        }

        private void OnMessageEvent(string message)
        {
            AddOutValue($"收到消息：{message}");
        }

        private void OnConnectStatusChangedEvent(bool status)
        {
            IsConnected = status;
            RaiseCommand();
            AddOutValue($"连接状态：{status}");
        }

        private void AddOutValue(string message, bool isError = false)
        {
            if (isError)
            {
                _logger.Error($"{message}");
            }
            else
            {
                _logger.Debug($"{message}");
            }
            SubText = $"{message}{Environment.NewLine}{SubText}";
        }

        private void RaiseCommand()
        {
            ConfigCommand.RaiseCanExecuteChanged();
            ConnectCommand.RaiseCanExecuteChanged();
            SendCommand.RaiseCanExecuteChanged();
        }
    }
}
